﻿using System;

namespace Safebrowsing
{
    /// <summary>
    /// Policy for the maintenance worker.
    /// </summary>
    public class MaintenancePolicy
    {
        /// <summary>
        /// Initilizes properties with default periods and delays.
        /// </summary>
        public MaintenancePolicy()
        {
            FirstRequestDelay = new TimeSpan(0,2,0); // 2 minutes
            DefaultRequestPeriod = new TimeSpan(0,20,0); // 20 minutes
            ErrorIntervals = new TimeSpan[5];
            ErrorIntervals[0] = new TimeSpan(0,1,0); // 1 minute
            ErrorIntervals[1] = new TimeSpan(0,30,0); // 30 minutes
            ErrorIntervals[2] = new TimeSpan(1,0,0); // 1 hour
            ErrorIntervals[3] = new TimeSpan(2,0,0); // 2 hours
            ErrorIntervals[4] = new TimeSpan(8,0,0); // 8 hours
        }

        /// <summary>
        /// Delay before first request after maintenance worker is initialized.
        /// Default value is 120 seconds.
        /// </summary>
        public TimeSpan FirstRequestDelay { get; set; }

        /// <summary>
        /// Default request period (seconds). Used if server did not specify next request time.
        /// Default value is 20 minutes.
        /// </summary>
        public TimeSpan DefaultRequestPeriod { get; set; }

        /// <summary>
        /// Array containing intervals client must wait after error occur.
        /// Array index + 1 -- error number.
        /// Value is delay in seconds.
        /// 
        /// Default values:
        /// ErrorIntervals[0] = new TimeSpan(0,1,0); // 1 minute
        /// ErrorIntervals[1] = new TimeSpan(0,30,0); // 30 minutes
        /// ErrorIntervals[2] = new TimeSpan(1,0,0); // 1 hour
        /// ErrorIntervals[3] = new TimeSpan(2,0,0); // 2 hours
        /// ErrorIntervals[4] = new TimeSpan(8,0,0); // 8 hours
        /// 
        /// If the client receives an error -- it waits for one minute before next request
        /// If it receives two errors in a row -- it waits for 30 minutes
        /// If it receives 3rd error -- it waits for 60 minutes
        /// If it receives 4rd error - it waits for 120 minutes
        /// If it receives 5rd error and more -- it waits for 480 minutes.
        /// 
        /// Once the client receives successfull HTTP replies -- error state is reset
        /// </summary>
        public TimeSpan[] ErrorIntervals { get; set; }
    }
}